package com.qiang.manager.dao;

import com.qiang.manager.domain.Student;

import java.util.ArrayList;

public class StudentDaoImp implements IstudentDao {
    //创建学生集合
    private static ArrayList<Student> array = new ArrayList<Student>();

    static {
        Student st1 = new Student("001", "张三", 23, "三年级一班");
        Student st2 = new Student("002", "李四", 24, "三年级二班");
        array.add(st1);
        array.add(st2);
    }

    @Override
    public void addStudent(Student student) {
        array.add(student);

    }

    @Override
    public void deleteStudent(String id) {
        int index = getIndex(id);
        array.remove(index);

    }

    @Override
    public void updateStudent(String id, Student newstudent) {
        int index = getIndex(id);
        array.set(index, newstudent);

    }

    @Override
    public ArrayList<Student> findStudent() {
        return array;
    }

    //getindex方法
    public int getIndex(String id) {
        int index = -1;
        //遍历集合
        for (int i = 0; i < array.size(); i++) {

            //判断非空，并且是否跟输入的id相同
            if (array.size() != 0 && (array.get(i).getId()).equals(id)) {
                index = i;
                return index;
            }
        }
        return index;
    }
}
